package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.DayShop;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IReachNotFg;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IReachYetFg;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.DetailActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.HistoryReachActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.ReachAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseFragmentActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseFragmentPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/6 13:26
 * 邮箱：zhanghuaiha@gmail.com
 */

public class ReachNotPresenter extends BaseFragmentPresenter<IReachNotFg> {
    private DetailActivity ac;
    private RecyclerView recycler;
    private LinearLayoutManager linearLayoutManager;
    private ReachAdapter adapter;
    private String udid;
    private String eid;
    private String versionName;
    List<DayShop> list;
    private TextView search;
    public ReachNotPresenter(BaseFragmentActivity context) {
        super(context);
    }

    public void loadContacts(List<DayShop> list){
        ac = getView().getAc();
        recycler = getView().getRecycler();
        search = getView().getSearch();
        linearLayoutManager = new LinearLayoutManager(mContext);
        recycler.setLayoutManager(linearLayoutManager);
        setAdapter();
        loadData(list);
    }

    private void setAdapter() {
        if (adapter == null) {
            adapter = new ReachAdapter(mContext);
        }
        recycler.setAdapter(adapter);
    }


    private void loadData(List<DayShop> list) {
        udid = App.SP.getString("udid", "");
        eid = App.SP.getString("id", "");
        versionName = ObtainVersion.getVersionName(mContext);
        adapter.addData(list);
        adapter.setItemClickListenter((dataBean, position) -> {
            Intent intent = new Intent(ac, HistoryReachActivity.class);
            intent.putExtra("sid",dataBean.getId());
            intent.putExtra("created",dataBean.getLay_time());
            intent.putExtra("name",dataBean.getName());
            ac.startActivity(intent);
        });
    //    getInfo();

        //加载更多
//        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
//                    if (lastVisiblePosition >= linearLayoutManager.getItemCount() - 1) {
//                        Log.e("layoutManager", "一定到底部了 准备加载更多");
//
//                    }
//                }
//            }
//        });
    }

    private String name,location;

    public void searh(String tv,List<DayShop> list){
        List<DayShop> filterDateList = new ArrayList<>();
        if (tv.equals("取消")) {
            search.setText("");
            filterDateList = list;
        }else {
            String s = search.getText().toString().trim();
            if (TextUtils.isEmpty(s)) {
                filterDateList = list;
            }else {
                if (!list.contains(s)) {
                    filterDateList.clear();
                    for (DayShop info : list) {
                        name = info.getName();
//                        location = info.getSpecific_address();
                        if (name.contains(s)) {
                            int i = name.indexOf(s);
                            System.out.println(i);
                            filterDateList.add(info);
                        }
                    }
                }
            }
        }

        adapter.addData(filterDateList);
    }

    public void getInfo(){
        String time = System.currentTimeMillis() + "";
        String encry = ObtainEncry.getDay_shop("2", time, versionName, NetWorkCodeInfo.DAY_SHOP, udid, "42", "1000", "0");
        RetrofitFactory.getmInstanse().mApi.getDay_Shop("2",time,versionName, NetWorkCodeInfo.DAY_SHOP,udid,"42","1000","0",encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<DayShop>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<DayShop>> dayShopBaseResponseEntity) {
                        if (dayShopBaseResponseEntity.code == 200) {
                            List<DayShop> data = dayShopBaseResponseEntity.data;
                            list = new ArrayList<DayShop>();
                            for (int i = 0; i < data.size(); i++) {
                                if (!data.get(i).getIs_ok().equals("1")) {
                                    list.add(data.get(i));
                                }
                            }
                            adapter.addData(list);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }
}
